﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using HCFramework.API.LINQ;

namespace HCFramework.Product.ProductManage
{
    public partial class Default : System.Web.UI.Page
    {
        public int? CatID
        {
            get { return ChooseProductCategory1.SelectedCategoryID; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ChooseProductCategory1.LoadTreeData();
            }
        }

        protected void btnFilter_Click(object sender, EventArgs e)
        {
            //string url = Request.Url.AbsoluteUri;
            //url = M.Http.SetPara(url, "ParentID", ChooseProductCategory1.SelectedCategoryID.HasValue ? ChooseProductCategory1.SelectedCategoryID.Value.ToString() : "");
            //Response.Redirect(url);
        }

        protected void RadGridMain_PreRender(object sender, EventArgs e)
        {
            LoadGridData();
        }
        public void LoadGridData()
        {
            HCDataContext db = new HCDataContext();
            var result = from a in db.Products
                         where CatID.HasValue ? CatID == a.CategoryID : true
                         orderby a.DateTimeInsert descending
                         select a;

            RadGridMain.DataSource = result;
            RadGridMain.DataBind();
        }
        protected void RadGridMain_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "Them")
            {
                if (CatID.HasValue) Response.Redirect("InsertUpdate.aspx?CategoryID=" + CatID.Value);
                else M.Message.ShowError("Please choose a category");
            }
            if (e.CommandName == "IsActive")
            {
                int id = int.Parse(e.CommandArgument + "");
                HCDataContext db = new HCDataContext();
                HCFramework.API.LINQ.Product product = C.ProductController.GetByID(id, db);
                product.IsActive = !product.IsActive;
                C.ProductController.Update(product, db);
            }
        }
        protected void RadGridMain_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                int productID = int.Parse((e.Item as GridDataItem)["ProductID"].Text);
                HCFramework.API.LINQ.Product product = C.ProductController.GetByID(productID);

                TextBox txtPrice = e.Item.FindControl("txtPrice") as TextBox;
                decimal price = decimal.Parse(txtPrice.Text);
                txtPrice.Text = price.ToString("N2");

                ImageButton ImageButton1 = e.Item.FindControl("ImageButton1") as ImageButton;
                ImageButton1.ImageUrl = "/Image/" + ImageButton1.ImageUrl + ".jpeg";

                Image productImage = e.Item.FindControl("ProductImage") as Image;
                productImage.ImageUrl = C.ProductImageController.GetMainImageURL(product);

            }
        }
        protected void LinkButtonDelete_Click(object sender, EventArgs e)
        {
            foreach (GridItem gridItem in RadGridMain.SelectedItems)
            {
                GridDataItem dataItem = (GridDataItem)gridItem;
                int id = int.Parse(dataItem["ProductID"].Text);

                C.ProductController.Delete(id);
            }
        }

        protected void LinkButtonUpdate_Click(object sender, EventArgs e)
        {
            foreach (GridDataItem item in RadGridMain.Items)
            {
                int id = int.Parse(item["ProductID"].Text);
                TextBox txtQuantity = item.FindControl("txtQuantity") as TextBox;
                TextBox txtPrice = item.FindControl("txtPrice") as TextBox;
                TextBox txtOrderDisplay = item.FindControl("txtOrderDisplay") as TextBox;

                HCDataContext db = new HCDataContext();
                HCFramework.API.LINQ.Product product = C.ProductController.GetByID(id, db);
                product.Price = decimal.Parse(txtPrice.Text);
                product.Quantity = int.Parse(txtQuantity.Text);
                product.OrderDisplay = int.Parse(txtOrderDisplay.Text);

                C.ProductController.Update(product, db);
            }
        }
    }
}